|
|
|
|
|
|
|
Solution 27
Universal Identifiers, Part 3 |
|
|
|
|
|
|
|
|
You've already seen in previous examples how to handle REFCLSID parameters, so you can be fairly confident that the problem does not lie with this parameter. |
|
|
|
|
|
|
|
|
The fact that the solution crashes illustrates one of the most common problems in working with new or complex API functionsthe tendency to jump to conclusions and create declarations without thought. The ppsz parameter is defined as an LPOLESTR * type. Believing that you can pass a Visual Basic string by reference is a nice thought, but it's wrong on many counts. |
|
|
|
|
|
|
|
|
To understand why, let's review some of the possible ways to pass a string parameter to an API function and what happens behind the scenes in each case. Figure S27-1 illustrates the overall process that goes on when you pass a string to an API function. (This subject is also discussed in Tutorial 5, "The ByVal Keyword: The Solution to 90 Percent of All API Problems," in Part III of this book.) |
|
|
|
|
|
|
|
|
Figure S27-1:
Passing strings to API functions |
|
|
|
|
|
|
|
|
Visual Basic stores strings internally in BSTR format. A BSTR is an OLE stringa string that is allocated by the OLE subsystem. A Visual Basic string variable contains a BSTR, which points to the start of the BSTR string data. The 4 bytes before the start of the string data contain the length of the string. The string data in a Visual Basic string is always stored in Unicode. When Visual Basic passes a string to an API function, it creates a temporary BSTR string that contains an ANSI |
|
|
|
|
|